package com.example.seventh;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;

public class MainActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getLayoutId();
        initData();
        bindWidget();
        initView();

    }
    private ArrayList<Drawable> mListDrawable;
    private ImageSwitcher mImageSwitcher;

    private Button mbtnPre;
    private Button mbtnNext;

    private  int index = 0;

    protected void getLayoutId() {
    }

    protected void initData() {
        mListDrawable = new ArrayList<Drawable>();
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run1));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run2));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run3));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run4));
        mListDrawable.add(getResources().getDrawable(R.mipmap.person_run5));
    }

    protected void bindWidget() {
        mImageSwitcher = findViewById(R.id.imageSwitcher);
        mbtnPre = findViewById(R.id.btnPre);
        mbtnNext = findViewById(R.id.btnNext);


    }

    protected void initView() {
        mbtnPre.setOnClickListener(this);
        mbtnNext.setOnClickListener(this);

        mImageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_in_left));
        mImageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this, android.R.anim.slide_out_right));
        mImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return new ImageView(MainActivity.this); // makeView返回的是当前需要显示的ImageView控件，用于填充进ImageSwitcher中。
            }
        });
        mImageSwitcher.setImageDrawable(mListDrawable.get(0));
    }

    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btnPre:
                index--;
                if(index<0)
                    index=mListDrawable.size()-1;//用于循环显示图片
                mImageSwitcher.setImageDrawable(mListDrawable.get(index)); //设定ImageSwitcher显示新图片
                break;
            case R.id.btnNext:
                index++;
                if(index>=mListDrawable.size())
                    index=0; //用于循环显示图片
                mImageSwitcher.setImageDrawable(mListDrawable.get(index));
                break;
            default:
                break;
        }
    }

    @Override
    public void onPointerCaptureChanged(boolean hasCapture) {

    }
}
